import express, { Request, Response } from "express";
import { body } from "express-validator";

import { requireAuth } from "../middlewares/require-auth";
import { validateRequest } from "../middlewares/validate-request";
import { Ticket } from "../models/ticket";

const router = express.Router();

router.post('/api/tickets', requireAuth, [
	body('title').not().isEmpty().withMessage("标题不能为空"),
	body('price').isFloat({ gt: 0 }).withMessage("价格有问题")
], validateRequest, async (req: Request, res: Response) => {
	const { title, price } = req.body;
	const ticket = Ticket.build({
		title,
		price,
		userId: req.currentUser!.id
	});
	await ticket.save();
	res.status(201).send(ticket);
	// res.sendStatus(200);
});

export { router as createTicketRouter };
